package com.core.shared;

import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: lxf
 * Date: 2016/4/21
 * Time: 16:04
 * To change this template use File | Settings | File Templates.
 */
public class ValidateCodeHandle {

    private static Map<String, String> validateCode = new HashMap<>();

    public static void save(String sessionId, String code) {
        if (StringUtils.isNotEmpty(code)) {
            validateCode.put(sessionId, code);
        }
    }

    private static String getValidateCode(String sessionId) {
        Object obj = validateCode.get(sessionId);
        if (obj != null) {
            return String.valueOf(obj);
        }
        return null;
    }

    public static boolean matchCode(String sessionId, String inputCode) {
        String saveCode = getValidateCode(sessionId);
        return StringUtils.isNotEmpty(saveCode) && saveCode.equals(inputCode);
    }

}
